/* enum */
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum';
/* util */
import { openAccurateTab } from '@src/util/platform';
import Platform from '@src/util/platform'
import i18n from '@src/locales'
import { ignorableWatch } from '@vueuse/core';
/* api*/
import { getNodeInstanceRelation } from '@src/api/ProjectManage.ts';

export function useOpenTab() {
  const fromId: any = window.frameElement?.getAttribute('id');

  // 打开任务详情页
  const openTaskManageViewTab = ({ id, projectId, taskType, taskBizId, taskFormName, taskFormId }: any) => {
    if (taskType === 'WORK_TASK') {
      // 工单类型的任务 跳转到工单详情
      if (taskBizId) {
        openAccurateTab({
          type: PageRoutesTypeEnum.PageTaskView,
          key: taskBizId,
          // titleKey: taskFormName,
          params: 'noHistory=1',
          reload: true,
          fromId,
        })
      } else {
        // @ts-ignore
        Platform.alert(i18n.t('common.projectManage.editTip2'))
      }
     
    }else if (taskType === 'PAAS_TASK') {
      if (taskBizId) {
        getNodeInstanceRelation({formTemplateBizId: taskFormId, formContentId:taskBizId }).then(res => {
          if(!res?.success) return Platform.alert(res?.message ?? '')

          // PAAS类型的任务，跳转到PAAS详情
          const { appId = '', processId = '' } = res?.data ?? {}

          let url = `/paas/#/template/detail?formId=${taskFormId}&formContentId=${taskBizId}&appId=${appId}`
          if(processId) url=`${url}&processId=${processId}`

          Platform.openTab({
            url,
            title: i18n.t('common.pageTitle.otherPageTitle.jobNotificationItem.title1'),
            id: `detail_view_${taskBizId}_${taskFormId}`,
            fromId,
          })
        })
      } else {
        // @ts-ignore
        Platform.alert(i18n.t('common.projectManage.editTip4'))
      }

    } else {
      openAccurateTab({
        type: PageRoutesTypeEnum.PageTaskManageView,
        params: `id=${id}&projectId=${projectId}`,
        reload: true,
        key: id,
        fromId,
      });
    }
  }

  // 打开项目详情页
  const openProjectViewTab = ({ projectTypeId, projectId }: any) => {
    openAccurateTab({
      type: PageRoutesTypeEnum.PageProjectManageView,
      params: `templateId=${projectTypeId}&id=${projectId}`,
      reload: true,
      fromId,
    });
  }

  return {
    openTaskManageViewTab,
    openProjectViewTab,
  }
}
